﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Entity;
using LotteryVote.Controls;
using LotteryVote.Utility;
using LotteryVote.Logic.Application;
using LotteryVote.Logic;
using LotteryVote.Logic.Web;

namespace LotteryVote
{
    public partial class AutoDropWater : CompanyPage
    {
        DropWaterFacade dropwaterFacade = new DropWaterFacade();
        IList<LotteryCompany> TodayCompanyList;
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                InitParams();
            });
        }

        void InitParams()
        {
            lv_page.InitPager();
            TodayCompanyList = TodayLotteryCompany.Instance.GetTodayCompany();
            if (!IsPostBack)
                BindAutoDropWater();
            lv_dropSearch.OnSearch += new EventHandler(lv_dropSearch_OnSearch);
            lv_addDropWater.OnAdded += new EventHandler((obj, arg) =>
            {
                BindAutoDropWater();
            });
        }

        void lv_dropSearch_OnSearch(object sender, EventArgs e)
        {
            Execute(() =>
            {
                BindAutoDropWater();
            });
        }

        void BindAutoDropWater()
        {
            int totalCount;
            rpt_AutoDrop.DataSource = GetDropWater(out totalCount);
            rpt_AutoDrop.DataBind();
            lv_page.BindPager(totalCount, LotterySystem.Current.PageItemCount);
        }

        IList<DropWater> GetDropWater(out int totalCount)
        {
            if (lv_dropSearch.IsSearch)
                return dropwaterFacade.ListAutoDropWaterByCondition(lv_dropSearch.Num, lv_dropSearch.CompanyId, lv_dropSearch.GamePlayWayId, lv_dropSearch.DropValue,
                    lv_dropSearch.Amount, LotterySystem.Current.PageItemCount, lv_page.CurrentPager, out totalCount);
            else
                return dropwaterFacade.ListAutoDropWater(LotterySystem.Current.PageItemCount, lv_page.CurrentPager, out totalCount);
        }

        public LotteryCompany FindCompany(object company)
        {
            return Execute(() =>
            {
                int companyId = Convert.ToInt32(company);
                return TodayCompanyList.Find(it => it.CompanyId == companyId);
            });
        }
        public string FindGamePlayWay(object gameplayway)
        {
            return Execute(() =>
            {
                int gameplaywayId = Convert.ToInt32(gameplayway);
                var gpw = LotterySystem.Current.FindGamePlayWay(gameplaywayId);
                return string.Format("{0}{1}", gpw.GameType.GameName,
                    (string.IsNullOrEmpty(gpw.PlayWay.WayName) ? string.Empty : (":=>" + GetGlobalResourceObject("Resource", gpw.PlayWay.WayName))));
            });
        }

        protected void rpt_AutoDrop_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Execute(() =>
            {
                int dropId = Convert.ToInt32(e.CommandArgument);
                if (e.CommandName == "Delete")
                {
                    dropwaterFacade.RemoveDropWater(dropId);
                    BindAutoDropWater();
                }
            });
        }
    }
}